package com.wolfking.converter.web;

import com.wolfking.converter.entity.SqlDataSource;
import com.wolfking.converter.entity.SqlDefine;
import com.wolfking.converter.service.SqlDataSourceService;
import com.wolfking.converter.service.SqlDefineService;
import com.wolfking.jeesite.config.SsoConfig;
import com.wolfking.jeesite.sign.Sign;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/sql/dataSource")
public class SqlDataSourceController {

    @Autowired
    private SqlDataSourceService sqlDataSourceService;

    @Autowired
    private SqlDefineService sqlDefineService;

    @Autowired
    private SsoConfig ssoConfig;

    @Sign(code = "sql:datasource:view")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public String list(Model model) {
        List<SqlDataSource> list = sqlDataSourceService.list();
        model.addAttribute("list", list);
        return "sql/datasource/list";
    }


    @Sign(code = "sql:datasource:view")
    @RequestMapping(value = "/edit", method = {RequestMethod.GET})
    public String editGet(Model model, @RequestParam(name = "id", required = false) Integer id) {
        if (Objects.nonNull(id)) {
            SqlDataSource byId = sqlDataSourceService.getById(id);
            if (Objects.isNull(byId)) {
                byId = new SqlDataSource();
            }
            model.addAttribute("datasource", byId);
        } else {
            model.addAttribute("datasource", new SqlDataSource());
        }
        return "sql/datasource/edit";
    }

    @Sign(code = "sql:datasource:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.POST})
    public String editPost(RedirectAttributes redirectAttributes, SqlDataSource data) throws Exception {
        if (StringUtils.isNoneBlank(data.getUrl())) {
            data.setUrl(data.getUrl().trim());
        }
        if (Objects.isNull(data.getId())) {
            sqlDataSourceService.save(data);
            redirectAttributes.addFlashAttribute("message", "添加数据源成功");
        } else {
            redirectAttributes.addFlashAttribute("message", "修改数据源成功");
            sqlDataSourceService.updateById(data);
        }
        return ssoConfig.getRedirectWithPath("/sql/dataSource/list");
    }

    @Sign(code = "sql:datasource:edit")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init(RedirectAttributes redirectAttributes, @RequestParam Integer id, @RequestParam String name) {
        try {
            SqlDataSource sqlDataSource = sqlDataSourceService.getById(id);
            if (sqlDataSource == null) {
                redirectAttributes.addFlashAttribute("messageType", "danger");
                redirectAttributes.addFlashAttribute("message", "数据源不存在");
            } else if (!sqlDataSource.getIsAble()) {
                redirectAttributes.addFlashAttribute("messageType", "danger");
                redirectAttributes.addFlashAttribute("message", "数据源不可用");
            } else {
                sqlDataSourceService.updateById(sqlDataSource);
                redirectAttributes.addFlashAttribute("message", String.format("初始化数据源【%s】成功", name));
            }
        } catch (Exception e) {
            log.error("", e);
            redirectAttributes.addFlashAttribute("messageType", "danger");
            redirectAttributes.addFlashAttribute("message", String.format("初始化数据源【%s】失败", name));
        }
        return ssoConfig.getRedirectWithPath("/sql/dataSource/list");
    }


    @Sign(code = "sql:datasource:edit")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET})
    public String delete(RedirectAttributes redirectAttributes, @RequestParam("id") Integer id) {
        SqlDataSource sqlDataSource = sqlDataSourceService.getById(id);

        if (sqlDataSource == null) {
            redirectAttributes.addFlashAttribute("messageType", "danger");
            redirectAttributes.addFlashAttribute("message", "数据源不存在");
        } else {
            SqlDefine sqlDefine = new SqlDefine();
            sqlDefine.setDataSource(sqlDataSource.getName());
            if (sqlDefineService.countExample(sqlDefine) > 0) {
                redirectAttributes.addFlashAttribute("messageType", "danger");
                redirectAttributes.addFlashAttribute("message", "存在关联的查询，不能删除");
            } else {
                sqlDataSourceService.deleteById(id);
                redirectAttributes.addFlashAttribute("message", "删除数据源成功");
            }
        }
        return ssoConfig.getRedirectWithPath("/sql/dataSource/list");
    }
}
